package com.bjqg.bjqg01.bjqg.activity.address.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bjqg.bjqg01.bjqg.R;
import com.bjqg.bjqg01.bjqg.activity.BaseActivity;
import com.bjqg.bjqg01.bjqg.activity.address.adapter.AddressAdapter;
import com.bjqg.bjqg01.bjqg.activity.longrange.bean.AddressBean;
import com.bjqg.bjqg01.bjqg.activity.longrange.bean.AddressEventBean;
import com.bjqg.bjqg01.bjqg.adapter.LeftAdapter;
import com.bjqg.bjqg01.bjqg.appli.MyApplication;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by bjqg01 on 2018/8/31.
 */

public class AddressManagerActivity extends BaseActivity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.tv_top_title_drawer)
    TextView tvTopTitleDrawer;
    @BindView(R.id.tv_search_top_drawer)
    TextView tvSearchTopDrawer;
    @BindView(R.id.tv_new_address)
    TextView tvNewAddress;
    @BindView(R.id.rv_address_list)
    RecyclerView rvAddressList;
    @BindView(R.id.address_srl)
    SmartRefreshLayout addressSrl;

    List<AddressBean> list = new ArrayList<>();
    AddressAdapter addressAdapter;
    String finish;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addressmanager);
        ButterKnife.bind(this);
        initview();
        initpost();
        initreash();
    }

    private void initview() {
        tvTopTitleDrawer.setText("地址管理");
        tvSearchTopDrawer.setVisibility(View.INVISIBLE);
        finish = getIntent().getStringExtra("finish");
    }

    @OnClick({R.id.back, R.id.tv_new_address})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_new_address://新建
                Intent intent = new Intent(this, ChangeAddressActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void initpost() {
        for (int i = 0; i < 20; i++) {
            String name = "常常常" + i;
            String phone = "13800000000";
            String address = "天津市东丽区华明镇";
            list.add(new AddressBean(name, phone, address));
        }
        LinearLayoutManager leftManager = new LinearLayoutManager(this);
        leftManager.setOrientation(LinearLayoutManager.VERTICAL);
        addressAdapter = new AddressAdapter(list, AddressManagerActivity.this);
        rvAddressList.setLayoutManager(leftManager);
        rvAddressList.setAdapter(addressAdapter);

        addressAdapter.setOnItemClickListener(new AddressAdapter.onItemClickListener() {
            @Override
            public void onItem(int position) {
                if (finish.equals("yes")){
                    EventBus.getDefault().post(new AddressEventBean("address", list.get(position).getName(), list.get(position).getPhone(), list.get(position).getAddress()));
                    finish();
                }

            }
        });

    }

    private void initreash() {
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                //全局设置主题颜色
                layout.setPrimaryColorsId(R.color.white, R.color.colorPrimary);
                //指定为经典Header，默认是 贝塞尔雷达Header
                return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
        //设置脚布局样式,全局有效
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });

        addressSrl.setEnableLoadmore(true);
        addressSrl.setEnableLoadmoreWhenContentNotFull(true);
//        addressSrl.autoRefresh();//进入界面自动刷新
        addressSrl.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {//上拉

            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {//下拉

            }
        });
    }
}
